package com.xbongbong.paas.handle;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.help.SaasListHelp;
import com.xbongbong.saas.service.SaasListService;
import com.xbongbong.saas.service.toolbox.help.SaasListDataPermissionHelp;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
@Component
public class OrderAnalysisDataHandle extends AbstractWebDataAnalysisHandle {
    private final static Logger LOGGER = LoggerFactory.getLogger(OrderAnalysisDataHandle.class);

    @Resource
    private SaasListService saasListService;
    @Resource
    private SaasListHelp saasListHelp;
    @Resource
    private SaasListDataPermissionHelp saasListDataPermissionHelp;

    @Override
    public PaasFormDataEsListVO analysisData(FormDataListDTO formDataListDTO) throws XbbException {
        PaasFormDataEsListVO formDataListVO;
        try {
            saasListService.setStatusFilter(formDataListDTO, ContractEnum.ARCHIVED.getAttr());
            List<ConditionsEntityExt> conditionList = formDataListDTO.getConditions();
            conditionList.addAll(saasListHelp.packageConditionBySubBusinessType(formDataListDTO.getSubBusinessType(),formDataListDTO.getCorpid(),formDataListDTO.getUserId(),formDataListDTO.getStatusFilter()));
            saasListDataPermissionHelp.listDataPermission(conditionList,formDataListDTO.getLoginUser(), XbbRefTypeEnum.ORDER.getCode(), formDataListDTO.getSubBusinessType());
            UserEntity userEntity = new UserEntity();
            BeanUtil.copyProperties(formDataListDTO.getLoginUser(), userEntity);
            // app端范围筛选
            saasListDataPermissionHelp.dingtalkRangeScreen(conditionList, userEntity, XbbRefTypeEnum.CONTRACT.getCode(), formDataListDTO.getSubBusinessType(), formDataListDTO.getPlatform());
            formDataListDTO.setConditions(conditionList);
            formDataListVO = getEsList(formDataListDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("OrderAnalysisDataHandle.analysisData 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return formDataListVO;
    }
}
